﻿/*
 * Copyright (c) 2008 Sculo, Yoann & Favréaux, Mathieu.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Lidgren.Library.Network;
using BBCommon;

namespace BBCommon
{
    public class PlayerScore
    {
        public int Shots;
        public int Kills;
        public int Hit;
        public int Deaths;        
    }
    public class Player
    {
        public PlayerCarac Carac;
        public ActorRTInfo Info;
        public NetConnection Connection;

        public PlayerScore score;
    }

    public class PlayerList
    {
        Hashtable List;

        public PlayerList()
        {
            List = new Hashtable();
        }

        public void AddPlayer(PlayerCarac carac, ActorRTInfo info, NetConnection co)
        {
            // Container creation
            Player player = new Player();
            player.Carac = carac;
            player.Info = info;

            if (co != null)
            {
                player.Connection = co;
                co.Tag = carac.ActorID;
            }

            player.score = new PlayerScore();

            // And finally add it to our list
            List.Add(player.Carac.ActorID, player);
        }

        // Create a player from a partially constructed PlayerCarac and return its ID
        public int CreatePlayer(PlayerCarac carac, NetConnection co)
        {
            carac.AssignID();
            ActorRTInfo info = new ActorRTInfo();
            
            // Set Physical infos
            info.Health = carac.HealthCap = 100;
            info.Mana = carac.ManaCap = 100;
            info.Weapon = 1;

            // Set position info
            info.Position = 1.0f;
            info.Velocity = 0.0f;

            // And add the player
            AddPlayer(carac, info, co);

            return carac.ActorID;
        }

        // Remove a player from the list
        public void DelPlayer(int id)
        {            
            List.Remove(id);
        }

        // Update PlayerInfo with info received from client
        public void UpdatePlayerInfo(ActorRTInfo info)
        {
            int id = info.Id;
            info.Id = id;

            GetPlayer(id).Info = info;
        }


        // Return a player from our list
        public Player GetPlayer(int id)
        {
            return (Player)List[id];
        }
        public Player GetPlayer(string nick, int? actorID)
        {
            // Look through our user base
            IDictionaryEnumerator en = List.GetEnumerator();
            while (en.MoveNext())
            {
                if (((Player)en.Value).Carac.Nick == nick)
                    return (Player)en.Value; 
            }              

            return null;
        }
        public IDictionaryEnumerator GetPlayerEnum()
        {
            return List.GetEnumerator();
        }
        public static int GetPlayerID(NetConnection co)
        {
            return (int)co.Tag;
        }
    }
}
